package com.hbnu.iostream;

import java.io.*;

/**
 * 需求：完成图片的复制，采用边读边写的方式（节约内存）
 *
 * @author kittychen
 * @since 2025-10-11 17:39
 */
public class PictureCopy {
    public static void main(String[] args) {
        String oriPath = "E:" + File.separator + "2311" + File.separator + "第三堂课" +
                File.separator + "imgs" + File.separator + "1.jpg";
        String desPath = "C:" + File.separator + "Users" + File.separator + "Administrator"
                + File.separator + "Desktop" + File.separator + "kittychen"
                + File.separator + "class2311" + File.separator + "pic_copy.jpg";
        File oriFile = new File(oriPath);
        File desFile = new File(desPath);

        OutputStream outputStream = null;
        InputStream inputStream = null;

        try {
            inputStream = new FileInputStream(oriFile);
            outputStream = new FileOutputStream(desFile);

            int temp;
            while ((temp = inputStream.read()) != -1) {
                outputStream.write(temp);
            }

            System.out.println("图片复制成功！");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
